<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Auth;

class UserAddress extends Model
{
    protected $table = 'user_address';
    protected $fillable = ['contact_name', 'contact_tel', 'area', 'address', 'is_default'];

    public static function boot() {
        parent::boot();
        
        static::creating(function($model) {
            $model->user_id = request()->user()->id;
        });
    }

    /**
     * 限定拥有者.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOwner($query)
    {
        return $query->where('user_id', Auth::user()->id);
    }

    /**
     * 限定拥有者.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeDefault($query)
    {
        return $query->where('is_default', 1);
    }

}
